package com.neurotech.pcc.ui

import android.os.Bundle
import android.support.v4.app.Fragment
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.NavHostFragment
import com.neurotech.pcc.R
import com.neurotech.pcc.other.Utils
import com.neurotech.pcc.other.darkStatusBarIcon
import com.neurotech.pcc.other.toastCenter
import kotlinx.android.synthetic.main.fragment_login.*

/**
 * login
 * @author shenhua
 */
class LoginFragment : Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_login, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loginBtn.setOnClickListener {
                        val name = usernameEt.text.toString()
            val pass = passwordEt.text.toString()
            if (TextUtils.isEmpty(name)) {
                toastCenter(getString(R.string.input_username))
                return@setOnClickListener
            } else if (TextUtils.isEmpty(pass)) {
                toastCenter(getString(R.string.input_password))
                return@setOnClickListener
            }
            if (name != "admin" && pass != "123456") {
                toastCenter(getString(R.string.password_invalid))
                return@setOnClickListener
            }
            Utils.hideKeyBoard(activity)
            Utils.login(context!!, name, pass)
            NavHostFragment.findNavController(this).navigate(R.id.mainFragment)
        }
    }

    override fun onResume() {
        super.onResume()
        darkStatusBarIcon(activity?.window?.decorView)
    }

}
